package com.gitee.feizns.quickstart.ding.talk.api.top.api.v2.department;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentListparentbydeptRequest;
import com.dingtalk.api.response.OapiV2DepartmentListparentbydeptResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.gitee.feizns.quickstart.ding.talk.utils.Utils;
import lombok.SneakyThrows;

import java.util.List;

/**
 * 获取指定部门的所有父部门列表（/topapi/v2/department/listparentbydept）
 * 接口路径：<a href="https://open.dingtalk.com/document/orgapp/query-the-list-of-all-parent-departments-of-a-department">获取指定部门的所有父部门列表（/topapi/v2/department/listsub）</a>
 * @link <a href="https://open.dingtalk.com/document/orgapp/query-the-list-of-all-parent-departments-of-a-department">服务端API 通讯录管理 部门管理 获取指定部门的所有父部门列表</a>
 * @author feizns
 * @since 2024/6/29
 */
public class ListParentByDept {

    /**
     * 获取指定部门的所有父部门列表
     * @param accessToken 访问令牌
     * @param deptId 部门 ID
     * @return {@link List }<{@link OapiV2DepartmentListsubResponse.DeptBaseResponse }>
     */
    @SneakyThrows
    public static List<Long> execute(String accessToken, Long deptId) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listparentbydept");
        OapiV2DepartmentListparentbydeptRequest req = new OapiV2DepartmentListparentbydeptRequest();
        req.setDeptId(deptId);
        OapiV2DepartmentListparentbydeptResponse rsp = client.execute(req, accessToken);
        return Utils.ifErrorThrow(rsp).getResult().getParentIdList();
    }

}
